package cn.kgc.mst.util;

import cn.kgc.mst.common.Const;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @author 韩
 * 切换数据源第二种方式
 */
@Slf4j
public class DataSourceAspect {

    public Object doAround(ProceedingJoinPoint pjp, DataSourceChnage dataSourceChnage) {

        Object retVal = null;
        //是否要切换数据源
        boolean selectedDataSource = false;
        try {
            if (dataSourceChnage != null) {
                //切换数据源确认
                selectedDataSource = true;
                if (dataSourceChnage.slave()) {
                    //切换数据源
                    MultipleDataSource.setDataSourceKey(Const.DATA_SOURCE_SLAVE);
                } else {
                    MultipleDataSource.setDataSourceKey(Const.DATA_SOURCE_MASTER);
                }
            }
            retVal = pjp.proceed();
        } catch (Throwable throwable) {
            log.error("切换数据源失败");
            throwable.printStackTrace();
        }
        return retVal;
    }

}
